package com.csun.cmny.security.core.authentication;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2021-01-18 19:50
 * @Description:
 */
public class WeiXinAuthenticationProvider implements AuthenticationProvider {

    private UserDetailsService userDetailsService;

    /**
     * Authenticate authentication.
     *
     * @param authentication the authentication
     *
     * @return the authentication
     *
     * @throws AuthenticationException the authentication exception
     */
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        WeiXinAuthenticationToken authenticationToken = (WeiXinAuthenticationToken) authentication;

        String code = (String) authenticationToken.getPrincipal();
        String nickName = (String) authenticationToken.getCredentials();
        String avatar = (String) authenticationToken.getAvatar();
        String gender = (String) authenticationToken.getGender();
        String city = (String) authenticationToken.getCity();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "wx");
        jsonObject.put("code", code);
        jsonObject.put("nickName", nickName);
        jsonObject.put("avatar", avatar);
        jsonObject.put("gender", gender);
        jsonObject.put("city", city);
        String jsonStr = JSONObject.toJSONString(jsonObject);

        UserDetails user = userDetailsService.loadUserByUsername(jsonStr);

        if (user == null) {
            throw new InternalAuthenticationServiceException("无法获取用户信息");
        }

        WeiXinAuthenticationToken authenticationResult = new WeiXinAuthenticationToken(user, nickName, avatar, gender, city, user.getAuthorities());

        authenticationResult.setDetails(authenticationToken.getDetails());

        return authenticationResult;
    }

    /**
     * Supports boolean.
     *
     * @param authentication the authentication
     *
     * @return the boolean
     */
    @Override
    public boolean supports(Class<?> authentication) {
        return WeiXinAuthenticationToken.class.isAssignableFrom(authentication);
    }

    /**
     * Gets user details service.
     *
     * @return the user details service
     */
    public UserDetailsService getUserDetailsService() {
        return userDetailsService;
    }

    /**
     * Sets user details service.
     *
     * @param userDetailsService the user details service
     */
    public void setUserDetailsService(UserDetailsService userDetailsService) {
        this.userDetailsService = userDetailsService;
    }
}
